﻿package com.element 
{
	import com.event.EventZheng;
	import com.staticInfo.EventConst;
	import com.utils.DebugTrace;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author zhengzihua
	 */
	public class ClipMenu extends Sprite
	{
		private var _obj:Clips;//存储菜单对应的物体
		private var useWidth:int = 120;
		private var useHeight:int = 60;
		public static var instance:ClipMenu;
		
		public function ClipMenu(single:SingletonEnforceMenu) 
		{
			mouseEnabled = true;
			mouseChildren = true;
			this.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
					if (e.localX > useWidth || e.localX < 0 || e.localY < 0 || e.localY > useHeight) {
						hideClipMenu();
					}
				})
		}
		/**
		 * 重组menu
		 * @param	arr	菜单列表
		 */
		public function reformLayout(arr:Array):void {
			var arrNum:int = arr.length;
			for (var i:int = 0; i < arrNum; i++ ) {
				switch(arr[i]) {
					case "move":
						addMenu(onMoveClick, "移动", 3, 5);
						break;
					case "del":
						addMenu(onDelClick, "删除", 3, 30);
						break;
					case "dirAngle":	
						addMenu(onAngleClick, "摆放方向", 3, 55);
						break;
						
					default:
						DebugTrace.dtrace("code error ClipMenu.as:菜单没有这样的操作。")
				}
			}
		}
		public static function getInstance():ClipMenu {
			if (instance == null) {
				instance = new ClipMenu(new SingletonEnforceMenu());
			}else {
				DebugTrace.dtrace("code info ClipMenu.as:该实例已经存在")
			}
			return instance;
		}
		private function addMenu(fun:Function,str:String,cx:int,cy:int):void {
			var btn:BtnMenu = new BtnMenu();
			btn.addEventListener(MouseEvent.CLICK, fun);
			btn.txt.text = str;
			addChild(btn);
			btn.x = cx;
			btn.y = cy;			
		}
		private function onMoveClick(e:MouseEvent):void 
		{
			throwEvent("move");
			hideClipMenu();
		}
		private function onDelClick(e:MouseEvent):void 
		{
			throwEvent("del");
			hideClipMenu();
		}
		private function onAngleClick(e:MouseEvent):void {
			throwEvent("dirAngle");
			hideClipMenu();
		}
		private function hideClipMenu():void {
			parent.removeChild(this);
		}
		private function throwEvent(str:String):void {
			var event:EventZheng = new EventZheng(EventConst.EVENT_CLIPMENU_MAIN);
			event.obj.type = str;
			event.obj.clip = _obj;
			parent.dispatchEvent(event);
		}
		/**
		 * 布局
		 */
		private	function layout():void {
			
		}
		
		public function get obj():Clips { return _obj; }
		
		public function set obj(value:Clips):void 
		{
			_obj = value;
		}
		
	}
}
class SingletonEnforceMenu{}